package org.infodavid.common.persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import org.infodavid.common.dto.IUserConnectionListItem;
import org.infodavid.common.impl.model.criteria.UserConnectionCriteriaImpl;
import org.infodavid.common.model.IUserConnection;
import org.infodavid.common.model.UserConnectionTest;
import org.infodavid.common.model.criteria.IUserConnectionCriteria;

/**
 * The Class UserConnectionDataServiceTest.
 */
@SuppressWarnings({
	"null"
})
public class UserConnectionDataServiceTest extends
    AbstractDataServiceTest<IUserConnectionListItem,IUserConnection,Long> {

	/**
	 * The Constructor.
	 */
	public UserConnectionDataServiceTest() {
		super(new UserConnectionTest(), UserConnectionDataService.getInstance());
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see
	 * org.infodavid.common.persistence.AbstractDataServiceTest#modify(org.infodavid.common.model.IDataObject
	 * )
	 */
	@Override
	public void modify(final IUserConnection item) throws Exception {
		item.setComment("ModifiedComment");
	}

	/**
	 * Test get instance.
	 */
	@Test
	public final void testGetInstance() {
		Assert.assertEquals("Not a singleton", UserConnectionDataService.getInstance(),
		    UserConnectionDataService.getInstance());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteria() throws Exception {
		final List<IUserConnectionListItem> results = new ArrayList<IUserConnectionListItem>();
		final IUserConnectionCriteria criteria = new UserConnectionCriteriaImpl();
		final long count =
		    ((UserConnectionDataService)getService()).find(getSession(), results, criteria);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingAllCriterions() throws Exception {
		final Collection<IUserConnection> inserted = getTestData();
		final IUserConnection connection = inserted.iterator().next();
		final List<IUserConnectionListItem> results = new ArrayList<IUserConnectionListItem>();
		final IUserConnectionCriteria criteria = new UserConnectionCriteriaImpl();

		criteria.setUserKey(connection.getInitiator().getKey());
		criteria.setRequested(Boolean.TRUE);
		criteria.setStatus(connection.getApprovalStatus());

		final long count =
		    ((UserConnectionDataService)getService()).find(getSession(), results, criteria);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 1, results.size());
		Assert.assertEquals("Not all data has been found", 1, count);
		Assert.assertEquals("Wrong data found", connection, results.get(0));
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNullCollection() throws Exception {
		final IUserConnectionCriteria criteria = new UserConnectionCriteriaImpl();
		Exception exception = null;

		try {
			((UserConnectionDataService)getService()).find(getSession(), null, criteria);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNotNull("No exception thrown", exception);
		Assert.assertEquals("Wrong exception thrown", IllegalArgumentException.class,
		    exception.getClass());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNullCriteria() throws Exception {
		final List<IUserConnectionListItem> results = new ArrayList<IUserConnectionListItem>();
		final long count =
		    ((UserConnectionDataService)getService()).find(getSession(), results,
		        (IUserConnectionCriteria)null);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingPage() throws Exception {
		final List<IUserConnectionListItem> results = new ArrayList<IUserConnectionListItem>();
		final IUserConnectionCriteria criteria = new UserConnectionCriteriaImpl();

		criteria.setPageStart(Long.valueOf(0));
		criteria.setPageSize(Integer.valueOf(5));

		final long count =
		    ((UserConnectionDataService)getService()).find(getSession(), results, criteria);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", criteria.getPageSize().intValue(),
		    results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}
}
